const http = require('http');
const fs = require('fs');
const path = require('path');


const server = http.createServer();

server.on('request', (req, res) => {

    //获取客户端的url地址
    // /clock/index.html
    // /clock/index.css
    // /clock/index.js
    const url = req.url;
    // 把请求的url地址映射为具体文件的存放路径
    // const fpath = path.join(__dirname, url);
    // 优化请求路径
    let fpath = '';
    if (url === '/') {
        fpath = path.join(__dirname, '/clock/index.html');
    } else {
        fpath = path.join(__dirname, '/clock', url);
    }


    // 根据‘映射’过来的文件路径读取文件内容
    fs.readFile(fpath, 'utf-8', (err, dataStr) => {
        if (err) return res.end('404 no found');
        res.end(dataStr);
    })

})

server.listen(8080, () => {
    console.log('server running at http://127.0.0.1:8080');
})